package com.cn.demo.controller;

import com.cn.demo.entity.Organization;
import com.cn.demo.entity.Student;
import com.cn.demo.entity.Teacher;
import com.cn.demo.service.TeacherService;
import com.cn.demo.utils.Result;
import com.cn.demo.utils.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
;

@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @PostMapping(value = "add")
    public Result add(@RequestBody Teacher teacher) {
        if (!StringUtils.isPhone(teacher.getTelephone())){
            return Result.error("手机格式错误");
        }
        return teacherService.add(teacher);
    }

    @GetMapping(value = "get")
    public Result get(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "5") Integer limit,
                      @RequestParam(value = "teacherId", required = false) String teacherId,
                      @RequestParam(value = "teacherName", required = false) String teacherName,
                      @RequestParam(value = "telephone", required = false) String telephone,
                      @RequestParam(value = "sex", required = false) String sex,
                      @RequestParam(value = "title", required = false) String title
    ) {
        Teacher teacher = new Teacher();
        teacher.setTeacherName(teacherName);
        teacher.setTeacherId(teacherId);
        teacher.setTelephone(telephone);
        teacher.setSex(sex);
        teacher.setTitle(title);
        return teacherService.get(page, limit, teacher);
    }


    @GetMapping(value = "/getByTeacherId/{teacherId}")
    public Result get(@PathVariable(value = "teacherId") String teacherId){
        return teacherService.getByTeacherId(teacherId);
    }

    @PostMapping(value = "/delete")
    public Result delete(@RequestBody String[] teacherIdList){
        if (teacherIdList.length == 0){
            return Result.error();
        }
        return teacherService.delete(teacherIdList);
    }


    @PutMapping(value = "/update")
    public Result update(@RequestBody Teacher teacher) {
        if (!StringUtils.isPhone(teacher.getTelephone())){
            return Result.error("手机格式错误");
        }
        return teacherService.update(teacher);
    }
}

